package com.yida.gwj.basis.controller.common;

import cn.hutool.core.bean.BeanUtil;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.core.utils.file.FileTypeUtils;
import com.yida.common.core.utils.poi.ExcelUtil;
import com.yida.common.core.web.controller.BaseController;
import com.yida.common.core.web.domain.AjaxResult;
import com.yida.common.core.web.page.TableDataInfo;
import com.yida.common.log.annotation.Log;
import com.yida.common.log.enums.BusinessType;
import com.yida.common.security.annotation.RequiresPermissions;
import com.yida.gwj.common.domain.dto.GwjBaseCommunityListDTO;
import com.yida.gwj.common.domain.dto.GwjBaseOwnerListDTO;
import com.yida.gwj.common.domain.request.GwjBaseCommunityQueryRequest;
import com.yida.gwj.common.domain.request.GwjBaseOwnerQueryRequest;
import com.yida.gwj.common.domain.request.GwjBaseOwnerUpdateRequest;
import com.yida.gwj.common.domain.vo.GwjBaseOwnerExcelVO;
import com.yida.gwj.common.service.IGwjBaseCommunityService;
import com.yida.gwj.common.service.IGwjBaseHouseService;
import com.yida.gwj.common.service.IGwjBaseOwnerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 投票楼栋Controller
 *
 * @author lqq
 * @date 2023-03-22
 */
@RestController
@Api(tags = "小区业主信息管理")
public class GwjBaseOwnerController extends BaseController {

	@Resource
	private IGwjBaseOwnerService gwjBaseOwnerService;

	@Resource
	private IGwjBaseHouseService gwjBaseHouseService;

	@Resource
	private IGwjBaseCommunityService gwjBaseCommunityService;

	@ApiOperation(value = "查询小区列表", response = GwjBaseCommunityListDTO.class)
	@GetMapping("/baseCommunity/list")
	@RequiresPermissions("gwj:basis:community:list")
	public TableDataInfo communityList(GwjBaseCommunityQueryRequest request) {
		startPage();
		List<GwjBaseCommunityListDTO> list = gwjBaseCommunityService.selectCommunityDtoList(request);
		return getDataTable(list);
	}

	@ApiOperation(value = "查询小区业主列表", response = GwjBaseOwnerListDTO.class)
	@GetMapping("/baseOwner/list")
	@RequiresPermissions("gwj:basis:owner:list")
	public TableDataInfo ownerList(GwjBaseOwnerQueryRequest request) {
		startPage();
		List<GwjBaseOwnerListDTO> list = gwjBaseHouseService.selectGwjBaseOwnerDtoListByCommunityId(request);
		return getDataTable(list);
	}

	@ApiOperation("修改业主信息")
	@RequiresPermissions("gwj:basis:owner:edit")
	@Log(title = "投票-投票业主信息", businessType = BusinessType.UPDATE, systemName = "gwj")
	@PutMapping("/baseOwner")
	public AjaxResult edit(@Validated @RequestBody GwjBaseOwnerUpdateRequest request) {
		return toAjax(gwjBaseOwnerService.updateGwjBaseOwnerByHouseId(request));
	}

	@ApiOperation("导出业主信息列表")
	@RequiresPermissions("gwj:basis:owner:export")
	@Log(title = "导出业主信息列表", businessType = BusinessType.EXPORT, systemName = "gwj")
	@PostMapping("/baseOwner/export/{communityId}")
	public void export(HttpServletResponse response, @PathVariable Long communityId) {
		List<GwjBaseOwnerListDTO> list = gwjBaseHouseService.selectGwjBaseOwnerDtoListByCommunityId(new GwjBaseOwnerQueryRequest() {{
			setCommunityId(communityId);
		}});
		ExcelUtil<GwjBaseOwnerExcelVO> util = new ExcelUtil<>(GwjBaseOwnerExcelVO.class);
		util.exportExcel(response, BeanUtil.copyToList(list, GwjBaseOwnerExcelVO.class), "投票业主信息数据");
	}

	@ApiOperation("业主信息导入")
	@ApiImplicitParam(name = "file", paramType = "form", value = "上传文件", dataType = "file", required = true)
	@Log(title = "业主信息导入", businessType = BusinessType.IMPORT, systemName = "gwj")
	@RequiresPermissions("gwj:basis:owner:import")
	@PostMapping("/baseOwner/import/{communityId}")
	public AjaxResult importGwjBaseOwner(MultipartFile file, @PathVariable Long communityId) throws Exception {
		if (file.isEmpty()) {
			return AjaxResult.error("导入业主信息文件不能为空");
		}
		if (!checkImportFile(file)) {
			return AjaxResult.error("不是指定的xlsx，xls导入类型文件");
		}
		String message = gwjBaseOwnerService.importGwjBaseOwner(file, communityId);
		return AjaxResult.success(message);
	}

	/**
	 * 检测导入文件类型是否合规
	 *
	 * @param file 文件
	 * @return /
	 */
	private boolean checkImportFile(MultipartFile file) {
		// 检查文件类型
		String type = FileTypeUtils.getExtension(file);
		// 限制的文件类型集合
		return StringUtils.inStringIgnoreCase(type, "xlsx", "xls");
	}
}
